package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.AfterSaleExperience;
import com.sl.au.entity.Attachment;
import com.sl.au.service.AfterSaleExperienceService;
import com.sl.au.service.AttachmentService;
import com.sl.au.service.OptionService;
import com.sl.au.service.SaleAreaService;


/*
 * 典型案例
 */

@RestController
@RequestMapping(value = "/ui/aftersaleexperience")
public class AfterSaleExperienceController extends BaseController {

	@Autowired
	AfterSaleExperienceService AfterSaleExperienceService;
	@Autowired
	AttachmentService attachmentService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("aftersaleexperience/experienceindex");
		return modelAndView;
	}

	@RequestMapping(value="getpage",method = RequestMethod.GET)
	public Map<String, Object> getServicePage(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");

		List<AfterSaleExperience> afterSaleExperiences=null;
		if(org.springframework.util.StringUtils.hasText(key)){
			afterSaleExperiences = AfterSaleExperienceService.getAfterSaleExperience(start, length, key, null, null);}
		else {
			afterSaleExperiences =AfterSaleExperienceService.getPage(start, length, null, null, "createTime");
		}
		Long count = AfterSaleExperienceService.getRepository().count();
		List<AfterSaleExperience>afterSaleExperiences2=new ArrayList<AfterSaleExperience>();
		for(AfterSaleExperience afterSaleExperience:afterSaleExperiences){
			String parentId=afterSaleExperience.getId();
			List<Attachment>attachments=attachmentService.getRepository().findByParentId(parentId);
			Integer attachCount=attachments.size();
			afterSaleExperience.setAttachCount(attachCount);
			afterSaleExperiences2.add(afterSaleExperience);
		}
		map.put("data", afterSaleExperiences2);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	////打开新增页面
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addExperience(HttpServletRequest request) {
		ModelAndView mv = createLayoutView("aftersaleexperience/experienceadd");

		return mv;
	}
	//编辑
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView editExperience(HttpServletRequest request) {
		ModelAndView mv = createLayoutView("aftersaleexperience/experienceedit");
		String id=request.getParameter("id");
		if(id!=null){
			AfterSaleExperience afterSaleExperience=AfterSaleExperienceService.getRepository().findById(id);
			mv.addObject("afterSaleExperience",afterSaleExperience);
		}

		return mv;
	}
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Map<String, Object> saveAfterSaleExperience(HttpServletRequest request, @RequestBody AfterSaleExperience afterSaleExperience) {
		Map<String, Object> map = new HashMap<String, Object>();
		AfterSaleExperienceService.getRepository().save(afterSaleExperience);
		map.put("afterSaleExperience",afterSaleExperience);
		return map;
	}
	///删除
	@RequestMapping(value = "del", method = RequestMethod.GET)
	public Map<String, Object> delCustomer(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		String ids=request.getParameter("ids");
		String id[]=ids.split(",");
		List<AfterSaleExperience> afterSaleExperiences=new ArrayList<AfterSaleExperience>();
		for(int i=0;i<id.length;i++){
			String afterSaleExperienceId = id[i];
			List<Attachment> attachments=attachmentService.getRepository().findByParentId(afterSaleExperienceId);
			attachmentService.getRepository().delete(attachments);
			AfterSaleExperience AfterSaleExperience=new AfterSaleExperience();
			AfterSaleExperience.setId(afterSaleExperienceId);
			afterSaleExperiences.add(AfterSaleExperience);
		}
		AfterSaleExperienceService.getRepository().deleteInBatch(afterSaleExperiences);;
		map.put("code", 0);
		return map;
	}
}
